import { baseRequestClient, realApiClient, requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    username: string;
    password: string;
    captcha?: string;
    checkKey?: string;
  }

  /** 登录接口返回值 */
  export interface LoginResponse {
    token: string;
    userInfo: {
      [key: string]: unknown;
      avator?: null | string;
      companyName?: string;
      creditCode?: string;
      email?: string;
      frozenEnd?: null | string;
      frozenReason?: null | string;
      frozenStart?: null | string;
      frozenStatus?: string;
      id: string;
      phone?: string;
      sex?: null | string;
      status?: string;
      username: string;
    };
    [key: string]: unknown;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }

  /** 真实API通用返回格式 */
  export interface RealApiResponse<T = any> {
    success: boolean;
    message: string;
    code: number;
    result: T;
    timestamp: number;
  }

  /** 企业注册接口参数 */
  export interface RegisterParams {
    userName: string;
    password: string;
    salt?: string;
    email?: string;
    companyName?: string;
    creditCode?: string;
    phone?: string;
    sex?: string;
    avator?: string;
    code?: string; // 验证码（如果API需要）
  }

  /** 注册返回结果 */
  export interface RegisterResult {
    empty: boolean;
    innerMap: Record<string, any>;
  }

  /** 手机号登录接口参数 */
  export interface PhoneLoginParams {
    mobile: string;
    smsmode: 1 | 2;
    captcha?: string;
  }

  /** 发送验证码接口参数 */
  export interface SendSmsParams {
    mobile: string;
    smsmode: 1 | 2; // 1-注册模式，2-登录模式
  }

  export interface UpdatePasswordParams {
    oldPassword: string;
    newPassword: string;
    code: string;
  }

  export interface UpdateProfileParams {
    email: string;
    companyName: string;
    creditCode: string;
    sex?: string;
    avator?: string;
  }

  export interface UploadResult {
    url: string;
    name?: string;
    message?: string;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return realApiClient.post<AuthApi.LoginResponse>(
    '/openapi/gtms/gtmsEnterpriseUser/ext/login',
    data,
  );
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return baseRequestClient.post('/auth/logout', {
    withCredentials: true,
  });
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return requestClient.get<string[]>('/auth/codes');
}

/**
 * 企业注册接口
 * 使用realApiClient，baseURL为空，直接请求/openapi路径
 */
export async function registerApi(data: AuthApi.RegisterParams) {
  return realApiClient.post<AuthApi.RealApiResponse<AuthApi.RegisterResult>>(
    '/openapi/gtms/gtmsEnterpriseUser/ext/register',
    data,
  );
}

/**
 * 手机号登录接口
 * 使用realApiClient，baseURL为空，直接请求/openapi路径
 */
export async function phoneLoginApi(data: AuthApi.PhoneLoginParams) {
  return realApiClient.post<AuthApi.LoginResponse>(
    '/openapi/gtms/gtmsEnterpriseUser/ext/phone/login',
    data,
  );
}

/**
 * 发送验证码接口
 * 使用realApiClient，baseURL为空，直接请求/openapi路径
 */
export async function sendSmsCodeApi(data: AuthApi.SendSmsParams) {
  return realApiClient.post<AuthApi.RealApiResponse<string>>(
    '/openapi/gtms/gtmsEnterpriseUser/ext/sms',
    data,
  );
}

export async function updatePasswordApi(data: AuthApi.UpdatePasswordParams) {
  return realApiClient.post<AuthApi.RealApiResponse<null>>(
    '/openapi/gtms/gtmsEnterpriseUser/ext/password',
    data,
  );
}

export async function updateProfileApi(data: AuthApi.UpdateProfileParams) {
  return realApiClient.post<AuthApi.RealApiResponse<null>>(
    '/openapi/gtms/gtmsEnterpriseUser/ext/profile',
    data,
  );
}

export async function uploadAvatarApi(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  return requestClient.post<AuthApi.UploadResult>(
    '/sys/common/upload',
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    },
  );
}
